package com.iot.mqtt.controller;

import com.iot.mqtt.service.MqttHookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HookController {
    @Autowired
    public MqttHookService MqttHookServiceImpl;

    @RequestMapping("/hook/handle")
    public String handle(@RequestBody Map<String,String> params){
        System.out.println(params);
        switch (params.get("action")){
            case "client_connected":
                MqttHookServiceImpl.connected(params);
                break;
            case "client_disconnected":
                MqttHookServiceImpl.disconnected(params);
                break;
        }
        return "ok";
    }

    @RequestMapping("/hook/loginAuth")
    public ResponseEntity<Map<String,Object>> loginAuth() throws IOException {
        Map<String,Object> map = new HashMap<>();
        map.put("name", "zhangsan");
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
//        System.out.println(params);
//        return "ok";
    }
}
